//
//  JYSettingController.m
//  JYFarm
//
//  Created by 花花 on 2017/10/12.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYSettingController.h"
#import "JYMineMenuCell.h"
#import "LYFielService.h"
#import "JYSetingPwdController.h"
#import "JYLoginController.h"
#import "BaseNavViewController.h"
#import "JYShowView.h"
@interface JYSettingController ()

@property(nonatomic)NSArray *titleArr;
@property(nonatomic)NSArray *imgArr;

@property(nonatomic)JYShowView *showView;

@end

static NSString *const JYSettingCellID = @"JYSettingCellID";
@implementation JYSettingController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];

}
- (void)initUI{

    self.title = @"设置";
    self.titleArr = @[@[@"修改密码"],@[@"检查更新",@"清除缓存"]];
    self.imgArr = @[@[@"mine_setting_modification"],@[@"mine_setting_update",@"mine_setting_cache"]];
    
    [self.tableView registerClass:[JYMineMenuCell class] forCellReuseIdentifier:JYSettingCellID];
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, kBtnHeight+30)];
    UIButton *logoutBtn  =  [UIButton jy_buttontitle:@"退出当前账号" titleColor:k3Color frame:CGRectMake(10, 30, Screen_Width-20, kBtnHeight) target:self action:@selector(logoutOnClick) fontSize:17 bgImageName:@"logout"];
    [footerView addSubview:logoutBtn];
    self.tableView.tableFooterView = footerView;

}
#pragma mark - <UITableViewDataSource,UITableViewDelegate>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   if(section == 0){
        return [self.titleArr[0] count];
    }
    return [self.titleArr[1] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JYMineMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:JYSettingCellID];
    
    if (indexPath.section == 0) {
        cell.titleLabel.text = [self.titleArr[0] safeObjectAtIndex:indexPath.row];
        cell.iconImage.image =[UIImage imageNamed:[self.imgArr[0] safeObjectAtIndex:indexPath.row]];
        }else{
        cell.titleLabel.text = [self.titleArr[1] safeObjectAtIndex:indexPath.row];
        cell.iconImage.image =[UIImage imageNamed:[self.imgArr[1] safeObjectAtIndex:indexPath.row]];
            if (indexPath.row ==1) {
                cell.subMoenyLabel.text = [self getCacheSize];
                cell.subMoenyLabel.textColor = k3Color;
            }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0 && indexPath.row == 0) {
        JYSetingPwdController *settingVc = [[JYSetingPwdController alloc]initWithIsSetingPwd:NO];
        [self.navigationController pushViewController:settingVc animated:YES];
    }
    
    if (indexPath.section == 1 && indexPath.row ==1) {
        
        [MBProgressHUD LY_ShowProgress:@"正在清除缓存"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD LY_HideHUD:YES];
        });
        [LYFielService clearCache:@"Library/Caches"];
        [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];

    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return 0;
    }
    return 10;
    
}
#pragma mark - Custom Methods
- (NSString *)getCacheSize{
    
    double size_m = [LYFielService folderSizeAtPath:@"Library/Caches"];
    NSString *cacheSizeStr = [NSString stringWithFormat:@"%.2fMB",size_m];
    if ([cacheSizeStr isEqual:@"nanMB"]) {
        cacheSizeStr = @"0.00MB";
    }
    return cacheSizeStr;
    
}
- (void)logoutOnClick{

    NSLog(@"退出当前账号");
    
    
    UIView *bgView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    bgView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
    
    [[UIApplication sharedApplication].keyWindow addSubview:bgView];
    _showView = [[JYShowView alloc] initWithFrame:CGRectMake((Screen_Width - showView_width)/2, (Screen_Height- showView_height)/2, showView_width, showView_height)];
    [bgView addSubview:_showView];
    JYWeakSelf;
    
    _showView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);
    _showView.alpha = 0;
    [UIView animateWithDuration:0.4 animations:^{
        weakSelf.showView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        weakSelf.showView.alpha = 1;
    }];
    
    [_showView showText:@"您真的要确认退出此账号吗" isSected:YES btnHandler:^(JYShowType type) {
    
        [UIView animateWithDuration:0.4 animations:^{
           
            weakSelf.showView.transform = CGAffineTransformMakeScale(.3f, .3f);
            weakSelf.showView.alpha = 0;
        }completion:^(BOOL finished) {
            if (type == JYShowTypeCancel) {
                [weakSelf.showView removeFromSuperview];
                [bgView removeFromSuperview];
            }else{
                NSLog(@"确认");
                
                [weakSelf logoutNetworkRequest];
                [bgView removeFromSuperview];
                [weakSelf.showView removeFromSuperview];
               
                
                JYLoginController *loginVc = [[JYLoginController  alloc]init];
                BaseNavViewController *nav = [[BaseNavViewController alloc]initWithRootViewController:loginVc];
                [self presentViewController:nav animated:YES completion:nil];
               
            }
        }];

            
        }];
        
    //test
//    [show showText:@"请确认已收货" isSected:NO btnHandler:^(JYShowType type) {
//        if (type == JYShowTypeCancel) {
//            
//            NSLog(@"取消");
//        }else{
//            NSLog(@"确认");
//            
//            
//        }
//    }];

//    删除后商品将不在显示，确认删除此类商品 吗？
//    
//   //        [show showText:@"删除后商品将不在显示,确认删除此商品吗？" isSected:YES btnHandler:^(JYShowType type) {
//            if (type == JYShowTypeCancel) {
//    
//                [show removeFromSuperview];
//                [bgView removeFromSuperview];
//                NSLog(@"取消");
//            }else{
//                NSLog(@"确认");
//    
//    
//            }
//        }];

//
//    BaseNavViewController *nav  = [[BaseNavViewController alloc]initWithRootViewController:[JYLoginController new]];
//    [self  presentViewController:nav animated:YES completion:nil];
}

- (void)logoutNetworkRequest{


}
@end
